/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import static dao.AlunoDAO.abreConexao;
import java.util.ArrayList;
import java.util.List;
import model.Aluno;
import model.Sessao;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import util.HibernateUtil;

/**
 *
 * @author Adriano
 */
public class SessaoDAO {
    private static Session session;
    private static Transaction transaction;

    @BeforeClass
    public static void abreConexao() {
        session = HibernateUtil.getSessionFactory().getCurrentSession();
        transaction = session.beginTransaction();
    }

    @AfterClass
    public void fechaConexao() {
        try {
            transaction.commit();
        } catch (Throwable e) {
            System.out.println("problema no commit" + e.getMessage());
        } finally {
            try {
                if (session.isOpen()) {
                    session.close();
                }
            } catch (Exception e) {
                System.out.println("problema ao fachar conexao" + e.getMessage());
            }
        }
    }


    public boolean adicionaSessao(Sessao sessao) {
        abreConexao();
        try {
            session.save(sessao);
            return true;
        } catch (Exception e) {
            System.out.println("Erro ao adicionar sessao! - " + e.getMessage());
            return false;
        } finally {
            fechaConexao();
        }
    }

    public Sessao getSessao(Long id) {
        String sql = "from Sessao sessao WHERE sessao.id = :id";
        abreConexao();
        Query consulta = session.createQuery(sql);
        consulta.setLong("id", id);

        Sessao sessaoPesquisada = (Sessao) consulta.uniqueResult();

       fechaConexao();
 
        return sessaoPesquisada;
    }
   
}
